﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_CongTacDoan : System.Web.UI.Page
{
    _dbDataContext _db = new _dbDataContext();
    SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
    protected void Page_Load(object sender, EventArgs e)
    {
        btn_close.Attributes.Add("onClick", "javascript:history.back(); return false;");//them su kien vao  button de quay ve trang truoc
        if (!IsPostBack)
        {
            string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"), "gold", 128);
            BindDataToControlKhongCoMaDoanVien(madoanvien);
            BindDataToGrid(madoanvien);
            txtid.Value = "";
        }
    }
    private void BindDataToControl(int id)
    {
        var val = _db.tblCongTacDoans.Single(r => r.ID == id);
        drlDonViTrucThuoc.SelectedValue = (val.DonViTrucThuoc!=null?val.DonViTrucThuoc.Trim():"");
        txtNgaycongtac.Text = string.Format("{0:dd/MM/yyyy}",val.NgayCongTac);
        drlCapUyHienTai.SelectedValue=(val.CapUyHienTai!=null?val.CapUyHienTai.ToString():"-1");
        drlCapUyKiem.SelectedValue = (val.CapUyKiem != null ? val.CapUyKiem.ToString() : "-1");
        drlChucVu.SelectedValue = (val.ChucVuHienTai != null ? val.ChucVuHienTai.ToString() : "-1");
        txtPhuCapChucVu.Text = val.PhuCapChucVu;
        txtCongViec.Text = val.CongViec;
        txtMoTaCongViec.Text = val.MoTaCongViec;
        drlNgachCongChuc.SelectedValue = (val.NgachCongChuc != null ? val.NgachCongChuc.ToString() : "-1");
        txtBacLuong.Text = val.BacLuong;
        txtHeso.Text = val.HeSo;
        txtNgayCap.Text = string.Format("{0:dd/MM/yyyy}", val.NgayCap);
        txtid.Value = id.ToString();
    }


    private void BindDataToGrid(string madoanvien)
    {
        var val = from p in _db.tblCongTacDoans
                  where p.MaDoanVien.Trim() == madoanvien
                  select new
                  {
                      ID=p.ID,
                      DonViCongTac=p.tblCoSoDoan.TenDonVi.Trim(),
                      NgayBatDau=string.Format("{0:dd/MM/yyyy}",p.NgayCongTac),
                      CapUyHienTai=p.tblDanhMuc.TenDanhMuc.Trim(),
                      CapUyKiem=p.tblDanhMuc1.TenDanhMuc.Trim(),
                      ChucVuDoan=p.tblDanhMuc2.TenDanhMuc.Trim(),
                  };
        grid.DataSource = val.ToList();
        grid.DataBind();

    }
    private void BindDataToControlKhongCoMaDoanVien(string madoanvien)
    {
        CatalogAccess.BindItemToDropDown(drlCapUyHienTai, 1011);
        CatalogAccess.BindItemToDropDown(drlCapUyKiem, 1011);
        CatalogAccess.BindItemToDropDown(drlChucVu, 9);
        drlChucVu.SelectedValue = _db.tblDoanViens.Single(r => r.MaDonVien.Trim() == madoanvien).ChucVuDoanID.ToString();
        CatalogAccess.BindItemToDropDown(drlNgachCongChuc, 1012);
        drlDonViTrucThuoc.Items.Clear();
        var val = _db.tblCoSoDoans.ToList();
        ListItem item = new ListItem("---Chọn đơn vị---", "-1");
        drlDonViTrucThuoc.Items.Add(item);
        foreach (var items in val)
        {
            item = new ListItem(items.TenDonVi.Trim(), items.MaDonVi.Trim());
            drlDonViTrucThuoc.Items.Add(item);
        }
        txtNgayCap.Text = "";
        txtNgaycongtac.Text = "";
        txtBacLuong.Text = "";
        txtHeso.Text = "";
        txtPhuCapChucVu.Text = "";
        txtCongViec.Text = "";
        txtMoTaCongViec.Text = "";
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (txtid.Value!="")
        {
            UpdateCongTacDoan(int.Parse(txtid.Value));
        }
        else
        {
            InsertCongTacDoan();
        }
    }

    private void UpdateCongTacDoan(int id)
    {
        try
        {
            var _tbl = _db.tblCongTacDoans.Single(r => r.ID == id);
            _tbl.DonViTrucThuoc = drlDonViTrucThuoc.SelectedValue.ToString().Trim();
            _tbl.NgayCongTac = CatalogAccess.ConvertToDate(txtNgaycongtac.Text);
            _tbl.CapUyHienTai = int.Parse(drlCapUyHienTai.SelectedValue.ToString());
            _tbl.CapUyKiem = int.Parse(drlCapUyKiem.SelectedValue.ToString());
            _tbl.ChucVuHienTai = int.Parse(drlChucVu.SelectedValue.ToString());
            _tbl.PhuCapChucVu = txtPhuCapChucVu.Text;
            _tbl.CongViec = txtCongViec.Text;
            _tbl.MoTaCongViec = txtMoTaCongViec.Text;
            _tbl.NgachCongChuc = int.Parse(drlNgachCongChuc.SelectedValue.ToString());
            _tbl.BacLuong = txtBacLuong.Text;
            _tbl.HeSo = txtHeso.Text;
            _tbl.NgayCap = CatalogAccess.ConvertToDate(txtNgayCap.Text);
            _db.SubmitChanges();
            lbl_message.ForeColor = System.Drawing.Color.Red;
            lbl_message.Text = "Cập nhật thành công.";
        }
        catch(Exception ex)
        {
            lbl_message.ForeColor = System.Drawing.Color.Red;
            //lbl_message.Text = "Cập nhật không thành công.";
            lbl_message.Text = ex.Message;
        }
    }
    private void InsertCongTacDoan()
    {
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"), "gold", 128);
        tblCongTacDoan _tbl = new tblCongTacDoan();
        _tbl.MaDoanVien = madoanvien;
        if (drlDonViTrucThuoc.SelectedValue.ToString() != "-1")
        {
            _tbl.DonViTrucThuoc = drlDonViTrucThuoc.SelectedValue.ToString().Trim();
        }
        _tbl.NgayCongTac = CatalogAccess.ConvertToDate(txtNgaycongtac.Text);
        if (drlCapUyHienTai.SelectedValue.ToString() != "-1")
        {
            _tbl.CapUyHienTai = int.Parse(drlCapUyHienTai.SelectedValue.ToString());
        }
        if (drlCapUyKiem.SelectedValue.ToString() != "-1")
        {
            _tbl.CapUyKiem = int.Parse(drlCapUyKiem.SelectedValue.ToString());
        }
        if (drlChucVu.SelectedValue.ToString() != "-1")
        {
            _tbl.ChucVuHienTai = int.Parse(drlChucVu.SelectedValue.ToString());
        }
        _tbl.PhuCapChucVu = txtPhuCapChucVu.Text;
        _tbl.CongViec = txtCongViec.Text;
        _tbl.MoTaCongViec = txtMoTaCongViec.Text;
        if (drlNgachCongChuc.SelectedValue.ToString() != "-1")
        {
            _tbl.NgachCongChuc = int.Parse(drlNgachCongChuc.SelectedValue.ToString());
        }
        _tbl.BacLuong = txtBacLuong.Text;
        _tbl.HeSo = txtHeso.Text;
        _tbl.NgayCap = CatalogAccess.ConvertToDate(txtNgayCap.Text);

        try
        {
            _db.tblCongTacDoans.InsertOnSubmit(_tbl);
            _db.SubmitChanges();
            lbl_message.ForeColor = System.Drawing.Color.Red;
            lbl_message.Text = "Cập nhật thành công.";
            BindDataToGrid(madoanvien);
        }
        catch
        {
            lbl_message.ForeColor = System.Drawing.Color.Red;
            lbl_message.Text = "Cập nhật không thành công.";
        }
    }
    
    protected void btn_add_Click(object sender, EventArgs e)
    {
        txtid.Value = "";
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"), "gold", 128);
        BindDataToControlKhongCoMaDoanVien(madoanvien);
    }
    protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "SelectDoanVien")
        {
            int id = int.Parse(e.CommandArgument.ToString());
            BindDataToControl(id);
        }
        else if (e.CommandName == "DeleteDoanVien")
        {
            DeleteCongTacDoan(int.Parse(e.CommandArgument.ToString()));
        }
    }
    private void DeleteCongTacDoan(int id)
    {
        var _tbl = _db.tblCongTacDoans.Single(r => r.ID == id);
        try
        {
            _db.tblCongTacDoans.DeleteOnSubmit(_tbl);
            _db.SubmitChanges();
            string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"), "gold", 128);
            BindDataToGrid(madoanvien);
            txtid.Value = "";
            BindDataToControlKhongCoMaDoanVien(madoanvien);
        }
        catch { }
    }
}